package pl.register.android.activities.correlation.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import pl.register.android.R;
import pl.register.android.activities.correlation.logic.Correlation;
import pl.register.android.activities.correlation.logic.CorrelationStudentsPresenter;
import pl.register.android.classes.Classes;
import pl.register.android.group.StudentsGroup;
import pl.register.android.homework.Homework;
import pl.register.android.lesson.LessonProgram;
import pl.register.android.student.Student;
import pl.register.android.util.StringUtils;
import pl.register.android.widget.listview.RefreshableAdapter;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */
public class CorrelationListAdapter extends RefreshableAdapter<Student>
        implements CompoundButton.OnCheckedChangeListener, View.OnClickListener, View.OnLongClickListener {

    private CorrelationView correlationView;

    private CorrelationStudentsPresenter presenter;

    private CorrelationLessonHeadersAdapter headerAdapterLesson;


    private Correlation correlation;

    public CorrelationListAdapter(Context context, CorrelationStudentsPresenter presenter, CorrelationView view) {
        super(context);
        this.presenter = presenter;
        this.correlationView = view;
        headerAdapterLesson = new CorrelationLessonHeadersAdapter(view.getHeadersView(), inflater, this, this);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_correlation_row, null);
            holder.vSeparator = convertView.findViewById(R.id.separator_section);
            holder.tvGroupName = (TextView) convertView.findViewById(R.id.group_name_text_view);
            holder.tvOrdinalNumber = (TextView) convertView.findViewById(R.id.ordinal_number_text_view);
            holder.btnStudentName = (Button) convertView.findViewById(R.id.student_button);
            holder.lessonAdapter = new CorrelationLessonAdapter(convertView, inflater, this);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        fillRow(holder, position);
        return convertView;
    }

    private void fillRow(ViewHolder holder, int position) {
        Student student = items.get(position);
        StudentsGroup studentGroup = correlation.getStudentGroup(student);
        int order = correlation.getStudentOrder(student);
        holder.tvOrdinalNumber.setText(StringUtils.build(order));
        holder.tvGroupName.setText(studentGroup.getType().getName().toUpperCase());
        holder.btnStudentName.setText(StringUtils.build(student.getFirstName(), " ", student.getLastName()));
        holder.btnStudentName.setTag(student);
        holder.btnStudentName.setOnLongClickListener(this);
        holder.btnStudentName.setOnClickListener(this);
        holder.lessonAdapter.refresh(correlation.getProgram(), student.getLessons());
        if (order == 1) {
            holder.vSeparator.setVisibility(View.VISIBLE);
        } else {
            holder.vSeparator.setVisibility(View.GONE);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.presence_check_box:
            case R.id.presence_1_check_box:
            case R.id.presence_2_check_box:
                Classes classes = (Classes) compoundButton.getTag();
                classes.setPresence(isChecked);
                presenter.saveClasses(classes);
                break;
            case R.id.done_check_box:
                Homework homework = (Homework) compoundButton.getTag();
                homework.setDone(isChecked);
                presenter.saveHomework(homework);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        if (correlation == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.student_button:
                Student student = (Student) view.getTag();
                correlationView.startStudentProfileActivity(student, correlation.getYear());
                break;
            case R.id.classes_schedule_button:
                LessonProgram lessonProgramClasses = (LessonProgram) view.getTag();
                correlationView.showClassesScheduleDialog(lessonProgramClasses, (Button) view);
                break;
            case R.id.homework_schedule_button:
                LessonProgram lessonProgramHomework = (LessonProgram) view.getTag();
                correlationView.showHomeworkScheduleDialog(lessonProgramHomework, (Button) view);
                break;
        }
    }

    @Override
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.classes_schedule_button:
                LessonProgram lessonProgramToRemove = (LessonProgram) view.getTag();
                correlationView.showDeleteLessonProgramConfirmationDialog(lessonProgramToRemove);
                break;
            case R.id.student_button:
                Student studentToRemove = (Student) view.getTag();
                StudentsGroup studentGroup = correlation.getStudentGroup(studentToRemove);
                correlationView.showDeleteStudentConfirmationDialog(studentToRemove, studentGroup);
                break;
        }
        return true;
    }

    public void refreshItems(Correlation correlation) {
        super.refreshItems(correlation.getAllStudents());
        this.correlation = correlation;
        headerAdapterLesson.refresh(correlation.getProgram());
    }

    public static class ViewHolder {
        public View vSeparator;
        public TextView tvOrdinalNumber;
        public TextView tvGroupName;
        public Button btnStudentName;

        public CorrelationLessonAdapter lessonAdapter;
    }

}